﻿﻿namespace NFinal
{
    public class ActionSearch
    {
        public static int Search(string requestedPath)
        {
			//重写虚拟目录
			if(requestedPath.StartsWith("/show"))
			{
				requestedPath = "/Add" + requestedPath.Remove(0,"/show".Length);
			}
			//重写虚拟路径,也可以把此部分放到find中
			switch (requestedPath)
			{
				//网站图标
				case "/favicon.ico": return 0;
				case "/Add":requestedPath = "/Index.html"; break;
				default: break;
			}
            //获取actionUrl
            string actionUrl = GetActionUrl(requestedPath);
            int find = GetActionId(actionUrl, requestedPath);
            return find;
        }
        /// <summary>
        /// 计算actionUrl
        /// </summary>
        /// <param name="requestedPath"></param>
        /// <returns></returns>
        private static string GetActionUrl(string requestedPath)
        {
            //找到最后一个.的位置
            int position = requestedPath.LastIndexOf('.');
            bool hasExtension = false;
            //判断是否有后缀（后缀首字母不能为数字，且长度必须大于0小于5）
            int extesionLength = requestedPath.Length - position-1;
            if (extesionLength > 0 && extesionLength < 5)
            {
                char ch = requestedPath[position + 1];
				//.后面不是数字
                if (ch > '9' || ch < '0')
                {
					hasExtension = true;
                }
            }
            //指向ActionUrl结尾的位置
            int pointer = 0;
            string actionUrl = requestedPath;
            bool hasGet = false;
            //如果有后缀
            if (hasExtension)
            {
				if(position>2)
				{
					hasGet = int.TryParse(requestedPath.Substring(position - 2, 2), out pointer);
				}
            }
            //如果没有后缀
            else
            {
				if(requestedPath.Length>2)
				{
					hasGet = int.TryParse(requestedPath.Substring(requestedPath.Length - 2, 2), out pointer);
				}
            }
            //取出ActionUrl
            if (hasGet)
            {
                if (pointer > -1 && pointer < requestedPath.Length)
                {
                    actionUrl = requestedPath.Substring(0, pointer + 1);
                }
            }
            return actionUrl;
        }
        /// <summary>
        /// 查询actionUrl
        /// </summary>
        /// <param name="app"></param>
        /// <param name="actionUrl"></param>
        /// <param name="requestedPath"></param>
        /// <returns></returns>
        private static int GetActionId(string actionUrl, string requestedPath)
        {
            int find = 0;
            //无参数
            switch (requestedPath)
            {
				case "/DDD/jqui/display.html":
                    find = 2; break;
				case "/DDD/login/show.html":
                    find = 3; break;
				case "/DDD/upload/show.html":
                    find = 6; break;
				case "/DDD/Public/Header.html":
                    find = 13; break;
				case "/DDD/Public/Footer.html":
                    find = 14; break;
				case "/DDD/Public/Navigator.html":
                    find = 15; break;
				case "/DDD/Public/KindEditor.html":
                    find = 16; break;
				case "/DDD/VCode/GetVerifyImage.html":
                    find = 17; break;
                default: find = 0; break;
            }
			//有参数
			if(find==0)
			{
				switch (actionUrl)
				{
					case "/DDD/IndexController/Index/":
						find = 1; break;
					case "/DDD/loginlogin/log":
						find = 4; break;
					case "/DDD/login/reg/":
						find = 5; break;
					case "/DDD/users/show/":
						find = 7; break;
					case "/DDD/users/edit/":
						find = 8; break;
					case "/DDD/users/insert/":
						find = 9; break;
					case "/DDD/users/delete/":
						find = 10; break;
					case "/DDD/Public/Success/":
						find = 11; break;
					case "/DDD/Public/Error/":
						find = 12; break;
					case "/DDD/VCode/VCheck/":
						find = 18; break;
					default: find = 0; break;
				}
			}
            return find;
        }
    }
}